package com.zj.component;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author zhangbo
 * @program: PDF-helper
 * @ClassName SpringConextComponent
 * @description: $
 * @date 2023-05-24 09:36:35
 */
@Component
public class SpringContextHelper implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextHelper.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 根据名字查找bean
     * @param name bean的名字
     * @return 结果
     */
    public static Object getBean(String name) {
        return applicationContext.getBean(name);
    }


    /**
     * 根据类对象获取bean对象
     * @param clazz 类对象
     * @param <T> 类型
     * @return 结果
     */
    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }
}
